﻿using System.Collections.ObjectModel;
using Asistente.Managers;
using Asistente.Model.General;

namespace Asistente.Model.Representation
{
    public class Screen : Element, IElement
    {
        public string Name { get; set; }
        public string Description { get; set; }

        private Collection<MasterDetail> masterDetails;
        public Collection<MasterDetail> MasterDetails
        {
            get { return masterDetails; }
            set { masterDetails = value; }}
    

        private Collection<Panel> panels;
        public Collection<Panel> Panels 
        { 
            get { return panels; } 
            set{ panels = value; }
        }
        
        private Collection<Action> actions;
        public Collection<Action> Actions { 
            get { return actions; } 
            set { actions = value; }
        }

        public Screen() 
        {
            panels = new Collection<Panel>();
            actions = new Collection<Action>();
            masterDetails = new Collection<MasterDetail>();
        }

        internal void DeleteMasterDetail(MasterDetail masterDetail)
        {
            if (MasterDetails.Contains(masterDetail))
                MasterDetails.Remove(masterDetail);
        }

        internal void DeletePanel(Panel panel)
        {
            if (Panels.Contains(panel))
                Panels.Remove(panel);
            foreach (MasterDetail masterDetail in MasterDetails)
            {
                masterDetail.DeletePanel(panel);           
            }
        }

        public Screen Clone()
        {
            Screen copy = (Screen)this.MemberwiseClone();
            Collection<Action> actions = new Collection<Action>();
            Collection<MasterDetail> masterDetails = new Collection<MasterDetail>();
            Collection<Panel> panels = new Collection<Panel>();

            foreach (var action in Actions)
            {
                actions.Add(action.Clone());
            }
            copy.Actions = actions;

            foreach (var masterDetail in MasterDetails)
            {
                masterDetails.Add(masterDetail.Clone());
            }
            copy.MasterDetails = masterDetails;

            foreach (var panel in Panels)
            {
                panels.Add(panel.Clone());
            }
            copy.Panels = panels;

            return copy;
        }

        public Action NewAction()
        {
            Action action = new Action();
            action.Name = Numerator.Instance.GetName(action.GetType());
            Actions.Add(action);
            return action;
        }

        public Action NewUseCaseAction(UseCase useCase)
        {
            Action action = new Action();
            action.Name = Numerator.Instance.GetName(action.GetType());
            action.UseCase = useCase;
            this.Actions.Add(action);
            return action;
        }

        private Panel CreatePanel(Panel panel)
        {
            panel.Name = Numerator.Instance.GetName(panel.GetType());
            Panels.Add(panel);
            return panel;
        }

        public Form NewForm()
        {
            return CreatePanel(new Form()) as Form;
        }

        public Filter NewFilter()
        {
            return CreatePanel(new Filter()) as Filter ;
        }

        public Graph NewGraph()
        {
            return CreatePanel(new Graph()) as Graph;
        }

        public Grid NewGrid()
        {
            return CreatePanel(new Grid()) as Grid;
        }

        public Report NewReport()
        {
            return CreatePanel(new Report()) as Report;
        }

        public MasterDetail NewMasterDetail()
        {
            MasterDetail masterDetail = new MasterDetail();
            masterDetail.Name = Numerator.Instance.GetName(masterDetail.GetType());
            masterDetails.Add(masterDetail);
            return masterDetail;
        }
    }
}
